package cn.kjcoder.redis.controller;

import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hkj
 * @email 1349091387@qq.com
 * @date 2022/1/21
 * 源码允许修改，并且本人十分乐意有 大佬 来对代码进行 随 意 调 教
 **/
@RestController
@Slf4j
@RequestMapping("/random")
public class RandomController {

    @Autowired
    private RedisTemplate redisTemplate;
    public static final String ZERO_PRIZE = "zero:prize";

    /**
     * 开奖时通过定时器调用该接口进行开奖
     * @return
     */
    @GetMapping("/prize")
    public List<Integer> prize(int num){
        try {
            SetOperations<String, Integer> setOperations = this.redisTemplate.opsForSet();
            //随机返回并删除set集合中的一个元素
            List<Integer> luckyPeople = setOperations.pop(ZERO_PRIZE, num);
            log.info("查询结果：{}", luckyPeople);
            return  luckyPeople;
        } catch (Exception e) {
            log.error("exception:", e);
        }
        return null;
    }
}
